package h0731;

import java.util.*;
/**
  * @description 找出经过特定点的路径长度 TODO 答案暂时不对，题目歧义
  * @author 不知名帅哥
  * @date 2024/7/31 16:53
  * @version 1.0
*/
public class LengthOfPath {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String path = sc.nextLine(); // 路径
        String mustPass = sc.nextLine(); // 必过点
        System.out.println(getMinDistance(path, mustPass));
    }

    private static int getMinDistance(String path, String mustPass) {
        int n = path.length();
        int m = mustPass.length();
        Map<Character, List<Integer>> positionMap = new HashMap<>();

        // 记录每个字符在路径中的索引位置
        for (int i = 0; i < n; i++) {
            char c = path.charAt(i);
            positionMap.computeIfAbsent(c, k -> new ArrayList<>()).add(i);
        }

        // dp数组，初始化为最大值
        int[][] dp = new int[m][n];
        for (int[] row : dp) {
            Arrays.fill(row, Integer.MAX_VALUE);
        }

        // 初始化dp数组的第一行
        for (int pos : positionMap.get(mustPass.charAt(0))) {
            dp[0][pos] = Math.min(pos, n - pos); // 起始点到第一个必过点的最短距离
        }

        // 动态规划计算最短路径
        for (int i = 1; i < m; i++) {
            char prevChar = mustPass.charAt(i - 1);
            char currChar = mustPass.charAt(i);
            for (int prevPos : positionMap.get(prevChar)) {
                for (int currPos : positionMap.get(currChar)) {
                    int dist = Math.min(Math.abs(currPos - prevPos), n - Math.abs(currPos - prevPos));
                    dp[i][currPos] = Math.min(dp[i][currPos], dp[i - 1][prevPos] + dist);
                }
            }
        }

        // 找到最后一行中的最小值
        int minDistance = Integer.MAX_VALUE;
        for (int pos : positionMap.get(mustPass.charAt(m - 1))) {
            minDistance = Math.min(minDistance, dp[m - 1][pos]);
        }

        return minDistance;
    }
}
